<?php
/*********************************************
  CPG DragonflyCMS, Copyright (c) 2011 by DragonflyCMS Dev Team
  http://dragonflycms.org
  Released under GNU GPL version 2 or any later version
**********************************************/

class SynFeed {

	public static $description;
	public static $link;
	public static $title;

	public static $category;
	public static $copyright;
	public static $language;
	public static $lastUpdated;
	public static $managingEditor;
	public static $pubDate;
	public static $rating;
	public static $webMaster;
	public static $version;

	public static $items = array();

	public static function template()
	{
		if ($status = Dragonfly_Net_Http::entityCache(md5(self::$pubDate), self::$pubDate)) {
			Dragonfly_Net_Http::headersFlush($status);
			exit;
		}
		global $MAIN_CFG, $cpgtpl;
		self::$title = htmlprepare(empty(self::$title) ? $MAIN_CFG['global']['sitename'] : self::$title);
		if (ctype_digit(self::$pubDate)) {
			self::$pubDate = date('D, d M Y H:i:s \G\M\T', self::$pubDate);
			Dragonfly_Net_Http::headersPush('Date: '.self::$pubDate);
		} else {
			self::$pubDate = date('D, d M Y H:i:s \G\M\T', time());
		}
		$cpgtpl->assign_vars(array(
			'S_DESCRIPTION'   => htmlprepare(empty(self::$description) ? $MAIN_CFG['global']['backend_title'] : self::$description),
			'S_LINK'          => Filter::domain(self::$link) ? self::$link : BASEHREF,
			'S_TITLE'         => self::$title,

			'S_CATEGORY'      => htmlprepare(self::$category),
			'S_COPYRIGHT'     => empty(self::$copyright) ? self::$title : htmlprepare(self::$copyright),
			'S_DOCS'          => 'http://cyber.law.harvard.edu/rss/rss.html',
			'S_IMG'           => BASEHREF.'images/'.$MAIN_CFG['global']['site_logo'],
			'S_GENERATOR'     => 'DragonflyCMS v10',
			'S_LANGUAGE'      => htmlprepare(empty(self::$language) ? $MAIN_CFG['global']['backend_language'] : self::$language),
			'S_LASTBUILDDATE' => self::$pubDate,
			'S_MANAGINGEDITOR'=> self::$managingEditor ? htmlprepare(self::$managingEditor) : false,
			'S_PUBDATE'       => self::$pubDate,
			'S_TTL'           => 60*24,
			'S_VERSION'       => self::$version && preg_match('#^[0-2]\.[0-9]$#', self::$version) ? self::$version : '2.0',
			'S_WEBMASTER'     => self::$webMaster ? htmlprepare(self::$webMaster) : false,
			'S_XML'           => '<?xml version="1.0" encoding="UTF-8"?>' .DF_EOL,
			'S_ATOM_LINK'     => BASEHREF.'?feed='.self::$category,
		));
		for ($i=0, $c=count(self::$items); $i<$c; ++$i) {
		if (empty(self::$items[$i]['title']) || Filter::domain(self::$items[$i]['link']) || empty(self::$items[$i]['description'])) { continue; }

		self::$items[$i]['description'] = BBCode::decodeAll(self::$items[$i]['description'], 1, true);
		self::$items[$i]['description'] = preg_replace('/(href|src)\=\"(?![hftp]{3,4}[^\"])/i', '$1="'.BASEHREF.'$2', self::$items[$i]['description']);

		$cpgtpl->assign_block_vars('item', array(
			'S_DESCRIPTION' => htmlprepare(self::$items[$i]['description'], false, ENT_QUOTES),
			'S_LINK'   => self::$items[$i]['link'],
			'S_GUID'   => isset(self::$items[$i]['guid']) ? self::$items[$i]['guid'] : self::$items[$i]['link'],
			'S_TITLE'   => htmlprepare(self::$items[$i]['title']),

			'S_AUTHOR'   => false,
			'S_CATEGORY'   => false,
			'S_COMMENTS'   => Filter::domain(self::$link) ? self::$link : false,
			'S_ENCLOSURE'   => false,
			'S_PUBDATE'   => empty(self::$items[$i]['time']) ? false : date('D, d M Y H:i:s \G\M\T', intval(self::$items[$i]['time'])),
			'S_QUID'   => false,
			'S_SOURCE'   => false
		));
		}
	}
}
